package com.aaa.hospital.aop;

import cn.hutool.core.date.DateUtil;
import com.aaa.hospital.mapper.Logmapper;
import com.aaa.hospital.pojo.domain.Emp;
import com.aaa.hospital.pojo.domain.LogData;
import com.aaa.hospital.result.AjaxResult;
import org.aspectj.lang.JoinPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;

/**
 * @Author: danghui
 * @Date: 2022/2/12 16:44
 * @Description: TODO
 */
public class HahaAOP {
    @Autowired
    private Logmapper logmapper;


    public void after(JoinPoint joinPoint, Log aaaLog, AjaxResult result){

        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        HttpServletResponse response = requestAttributes.getResponse();

        Emp emp = (Emp) request.getSession().getAttribute("emp");
        String ename = emp.getName();

        String    msg = aaaLog.value();
        Integer   code = result.getCode();
        String    addr = request.getRemoteAddr();
        Date date = DateUtil.date();
        Object[]  data = joinPoint.getArgs();

        LogData logData = new LogData(null, ename, msg, code, addr, date.toString(), Arrays.toString(data));
        logmapper.addLog(logData);

    }

}
